package com.example.demo.消费者;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author 杨光
 * @date 2021年08月05日 11:24
 */
@Component
public class MessageListener {
    //死信队列


    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange=@Exchange(name = "延时交换机",type = "direct"),
                    value = @Queue(name="woniu38.spring.boot.rabbit.queue"),
                    key = {"woniu38.spring.boot.rabbit.routingkey"}
            )
    })
    public void listen(String msg){
        System.out.println("收到消息："+msg);
    }


    @RabbitListener(queues = {"死信队列"})
    public void delay(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        System.out.println("收到延时队列的消息："+msg +":"+new Date());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
